/**
 * Created by apple on 2017/8/6.
 */

//写完行为后,定义响应,就是用户触发后,需要返回的响应,传入一个旧的状态,返回一个新的状态


const todo = (state,action) =>{
  switch (action.type){
    //添加一个待办项的时候,返回的数据
    case "ADD_TODO":
      return {
        id:action.id,
        text:action.text,//需要显示的文字
        completed:false//是否完成
      };
      //定义点击事件,点一次完成,再点一次 未完成
    case "TOGGLE_TODO":
      if(state.id!==action.id){
        return state
      }
      return Object.assign({},state,{
        completed:!state.completed
      });
    default:
      return state
  }
};
//state是传入旧状态,返回新状态
const todos = (state,action) =>{
  switch (action.type){
    case "ADD_TODO":
      return [
        ...state,
        todo(undefined,action)
      ]
    case "TOGGLE_TODO":
      return state.map(t =>todo(t,action))
    default:
      return state
  }
};

export default todos;